package com.example.chuntiao.cophook.ui;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.example.chuntiao.cophook.R;
import com.example.chuntiao.cophook.mytools.MyApp;

public class MainSetting extends AppCompatActivity {

    Switch swichServer;
    SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_setting);

        preferences = getSharedPreferences("data", MODE_PRIVATE);
        swichServer = (Switch) findViewById(R.id.swich_server);
        swichServer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                SharedPreferences.Editor editor = preferences.edit();

                if (isChecked) {
                    buttonView.setText("局域网");
                    MyApp.setHostIp(MyApp.LocalHost);
                    editor.putBoolean("is_lan", true);
                } else {
                    buttonView.setText("公网");
                    MyApp.setHostIp(MyApp.RemoteHost);
                    editor.putBoolean("is_lan", false);
                }
                editor.apply();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean isLan = preferences.getBoolean("is_lan", false);
        swichServer.setChecked(isLan);
    }
}
